layui.use(['jquery','layer','table','form','laydate'],function(){
    var $=layui.jquery;
    var layer=layui.layer;
    var table=layui.table;
    var form=layui.form;

    var createDate = '';
    //刷新
    refresh = function refresh(){
        location.reload();
    }
    //返回
    $('#goBack').click(function () {
        parent.tabParentChange(3);
    })
    $(document).click(function () {
        parent.$('.rightMenu').hide();
    });

    //模糊查询
    search = function search(){
        var userCode=$("input[name=userCode]").val();
        var reason=$("input[name=reason]").val();
        var days=$("#days option:selected").text();
        var type;
        if($("#type option:selected").text()=="前端"){
            type=$("#type option:selected").val();
        }else if($("#type option:selected").text()=="后端"){
            type=$("#type option:selected").val();
        }
        getTable(userCode,reason,days,type);
    }

    //高级检索
    retrieval=function retrieval(){
        layer.open({
            type : 2,
            title : "高级检索",
            area : [ '900px', '480px' ],
            shade : 0,
            offset: 'auto',
            content :"advanced-retrieval.html",
            btn:['确定','取消'],
            yes: function(index, layero){
                var child = layero.find('iframe').contents();
                var userCode= child.find("input[name=userCode]").val();
                var reason=child.find("input[name=reason]").val();
                var startDate=child.find("input[name=startDate]").val();
                var endDate=child.find("input[name=endDate]").val();
                var param = {
                    'userCode':userCode,
                    'reason':reason,
                    'startDate':startDate,
                    'endDate':endDate,
                }
                $.ajax({
                    url:"/closure/getClosureAdvancedRetrieval",
                    data:param,
                    type:"post",
                    dataType:"json",
                    success:function (data) {
                        demoTable(data.data);
                        layer.close(layer.index);
                    }
                })
            }
        })
    }


    //数据列表
    getTable("","","","")
    function getTable(userCode,reason,days,type){
        var param={
            'userCode':userCode,
            'reason':reason,
            'days':days,
            'type':type
        }
        $.ajax({
            url:"/closure/getAllClosure",
            data:param,
            type:"post",
            dataType:"json",
            success:function (data) {
                demoTable(data.data);
            }
        })
    }
    //表格
    function demoTable(data){
        table.render({
            elem:'#demoTable',
            title:"委托书",
            data:data,
            toolbar:'#button_ids',
            limit:10,
            page:{
                layout:['count','prev','page','next','limit','skip','refresh'],
                limits:[10,20,30],
                first:"首页",
                last:"尾页",
            },
            cols:[[
                {type:'checkbox'},
                {title: '序号',align:'center', templet: function (data) {
                        return data.LAY_INDEX;
                    }},
                {field:"userCode",title:"用户编号",align:'center'},
                {field:"startDate",title:"封停时间",align:'center'},
                {field:"endDate",title:"解封时间",align:'center'},
                {field:"reason",title:"封停原因",align:'center'},
                {field:"days",title:"封停天数",align:'center',templet:function(data) {
                        if(data.days==0){
                            return "已解封";
                        }else{
                            return data.days+"天";
                        }
                    }},
                {field:"type",title:"用户",align:'center',templet:function(data) {
                        if(data.type==2){
                            return "前端用户";
                        }else{
                            return "后端用户";
                        }
                    }},
                {field:'',title:"操作",align:'center',toolbar:'#button_caozuo'}
            ]]
        })
    }

    //删除、解封
    table.on('tool(demoTable)',function (obj) {
        var event = obj.event;
        if (event == "unshutdownTable"){
            getTime();
            layer.confirm('确认要解封吗?', {icon: 3, title:'提示'}, function(index){
                $.ajax({
                    url:"/user/editBeforeUserStatus",
                    data:{'status':2,'userId':obj.data.userId},
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        $.ajax({//删除封停记录
                            url:"/closure/delClosureById",
                            data:{'id':obj.data.id},
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                                var param ={
                                    'userId':obj.data.userId,
                                    'userCode':obj.data.userCode,
                                    'account':obj.data.account,
                                    'endDate':createDate,
                                    'reason':obj.data.reason+'(手动解封)',
                                    'days':obj.data.days,
                                    'type':obj.data.type,
                                }
                                $.ajax({//添加解封日志
                                    url:"/closure/addUnsealLog",
                                    data:param,
                                    type:'post',
                                    dataType:'json',
                                    success:function (data) {
                                        layer.msg("已解封");
                                        getTable("","","","");
                                    }
                                })
                            }
                        })
                    }
                })

            })
        }else if(event == "deleteTable"){
            layer.confirm('确认要删除吗?', {icon: 3, title:'提示'}, function(index){
                $.ajax({
                    url:"/closure/delClosureById",
                    data:{'id':obj.data.id},
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        layer.msg("已删除");
                        getTable("","","","");
                    }
                })
                layer.close(index);
            });
        }
    })

    automaticUnsealing();
    //设置定时器 每小时调用一次
    setInterval(automaticUnsealing,3600000);

    //自动解封
    function automaticUnsealing(){
        getTime();
        $.ajax({
            url:"/closure/getAllClosure",
            data:{},
            type:"post",
            dataType:"json",
            success:function (data) {
                //所有用户
                var endDate = data.data;
                for(var i=0;i<endDate.length;i++){
                    //如果封停时间小于等于当前时间解封
                    if(createDate >=endDate[i].endDate){
                        var userId = endDate[i].userId;
                        var id = endDate[i].id;
                        $.ajax({//修改用户的状态
                            url:"/user/editBeforeUserStatus",
                            data:{'status':2,'userId':userId},
                            type:'post',
                            dataType:'json',
                            success:function (data) {
                            }
                        })
                        //删除封停记录 添加解封日志
                        unseal(userId,id);
                    }
                }
            }
        })
    }

    function unseal(userId,id) {
        getTime();
        //根据userId查询用户信息
        $.ajax({
            url:"/closure/getClosureByUserId",
            data:{"userId":userId,"type":2},
            type:"post",
            dataType:"json",
            success:function (data) {
                //添加解封日志
                var param={
                    'userId':userId,
                    'userCode':data.data.userCode,
                    'account':data.data.account,
                    'endDate':createDate,
                    'reason':data.data.reason+'(自动解封)',
                    'days':data.data.days,
                    'type':data.data.type,
                }
                $.ajax({
                    url:"/closure/addUnsealLog",
                    data:param,
                    type:'post',
                    dataType:'json',
                    success:function (data) {
                        console.log("添加封停日志成功");
                    }
                })

            }
        })
        //删除封停记录
        $.ajax({
            url:"/closure/delClosureById",
            data:{'id':id},
            type:'post',
            dataType:'json',
            success:function (data) {
                console.log("删除封停记录成功");
                getTable("","","","");
            }
        })
    }


    //现在时间
    function getTime(){
        var date=new Date();
        var year=date.getFullYear();
        var month=date.getMonth()+1;
        var day=date.getDate();
        var hour=date.getHours();
        var minute=date.getMinutes();
        var second=date.getSeconds();
        var dateStr = year+"-"+zeroFilling(month)+"-"+zeroFilling(day)+" "+zeroFilling(hour)+":"+zeroFilling(minute)+":"+zeroFilling(second);
        createDate = dateStr;
    }
    function zeroFilling(data){
        if(data<10){
            data="0"+data;
        }
        return data;
    }

})